import {
  api,
  util
} from "../util";
import {
  router
} from '../router';
import authorize from './authorize.js'
const app = getApp();

const credit={
  /**
   * 加载帐号信息
   */
  get_account: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    };
    util.fetch("credit.account.get", param, callback, "3.0");
  },
  /**
   * 是否开通授信功能
   */
  judge_exist_account: (callback)=>{
    var entity = authorize.getEntity() || authorize.getDefault();
    var param = {
      Token: entity.manager.Token,
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    };
    util.fetch("credit.account.exist", param, callback, "3.0", "post","json",{},false);
  },
  /**
   * 加载列表
   */
  load_list: (page, accountId,callback) => {
    var entity = authorize.getEntity();
    var param = util.mergeJsonObject(page, {
      Token: entity.manager.Token,
      AccountId:accountId,
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    });
    util.post("credit.bill.list", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          callback(res.Content);
        }
      } else {
        util.alert(res.Content);
      }
    },"3.0");
  },
  /**
 * 加载帐号信息
 */
  get_bill_detail: (id,callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      BillId:id,
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    };
    util.fetch("credit.bill.detail", param, callback, "3.0");
  },
  /**
   * 支付账单
   * @accountId 账号
   * @billId 账单ids
   * @orderIds 订单ids
   * @amount 金额
   * @callback 回调
   */
  pay_bill: function (t, accountId, billId,orderIds, amount, callback){
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      Type: t,
      ProprietorId: app.dns.proprietorId,
      AccountId:accountId,
      BillIds: billId,
      OrderIds:orderIds,
      Amount: amount.toFixed(2),
      StoreId:entity.fkId
    };
    util.post("credit.repay.prepay",param,function(res){
      res=res.data;
      if (res.Success) {
        //credit.get_repay_info(res.Content.Id, billId, callback);
        router.navigate({
          path: "billPayComfirm",
          params: {
            billId: billId,
            repayId: res.Content.Id
          }
        });
      }
      else {
        util.alert(res.Message);
      }
    },"3.0",null,false);
  },
  /**
   * 获得发起支付的还款信息
   * @repayId 还款id
   * @billId 账单id
   * @callback 回调
   */
  get_repay_info: function (repayId, billId, callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      billId: billId,
      repayId: repayId,
      ProprietorId: app.dns.proprietorId,
      StoreId: entity.fkId
    };
    util.post("credit.repay.pay", param, function (res) {
      res=res.data;
      if(res.Success){
        if (callback != undefined && typeof (callback) == "function") {
          callback(res.Content);
        }
      }
      else{
        util.alert(res.Message);
      }
    }, "3.0",null,false);
  },
  /**
   * 获得账单包含的订单信息
   * @id 账单id
   * @callback 回调函数
   */
  get_bill_orders: (id, callback)=>{
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      Id: id,
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    };
    util.post("credit.bill.orders", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          callback(res.Content);
        }
      }
      else {
        util.alert(res.Message);
      }
    }, "3.0");
  },
  /**
  * 获得账单包含的订单信息
  * @page 分页信息
  * @accountId 账号id
  * @billId 账单id
  * @typeId 类型id
  * @callback 回调函数
  */
  get_bill_order_list: (page,accountId, billId,typeId,callback) => {
    var entity = authorize.getEntity();
    var param = util.mergeJsonObject(page, {
      Token: entity.manager.Token,
      AccountId: accountId,
      BillId:billId,
      TypeId:typeId,
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    });
    util.post("credit.bill.orderlist", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          callback(res.Content);
        }
      }
      else {
        util.alert(res.Message);
      }
    }, "3.0");
  },
  /**
   * 取消还款
   */
  cancelRepay: function (repayId, callback){
    var entity = authorize.getEntity();
    var param ={
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId,
      RepayId: repayId,
      Id: repayId
    };
    util.post("credit.repay.canel", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          callback(res.Content);
        }
      }
      else {
        util.alert(res.Message);
      }
    }, "3.0");
  }
}
export{
  credit
}